package com.ruoqing.gateway.filter;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.reactive.CorsWebFilter;
import org.springframework.web.cors.reactive.UrlBasedCorsConfigurationSource;
import org.springframework.web.util.pattern.PathPatternParser;
/**
	* @author YaoXian
	* @title: CorsConfig
	* @projectName ruoxi-blog
	* @description: 若兮微服务 - 利用网关全局过滤器,解决整个微服务跨域问题
	* @date 2020-12-02 23:07
	*/
@Configuration
public class CorsConfig {
		
		@Bean
		public CorsWebFilter corsFilter() {
				UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource(new PathPatternParser());
				source.registerCorsConfiguration("/**", buildConfig());
				return new CorsWebFilter(source);
		}
		
		private CorsConfiguration buildConfig() {
				CorsConfiguration corsConfiguration = new CorsConfiguration();
				//在生产环境上最好指定域名，以免产生跨域安全问题
				corsConfiguration.addAllowedOrigin("*");
				corsConfiguration.addAllowedHeader("*");
				corsConfiguration.addAllowedMethod("*");
				return corsConfiguration;
		}
}
